package com.qianxx.viewlibrary.chart.statistical;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;

import com.qianxx.viewlibrary.R;

import java.text.DecimalFormat;

/**
 * 水平数据统计条
 */
public class HorizontalStatisticalBar extends View implements Animator.AnimatorListener {
    //画笔
    private Paint paint;
    //柱状条景色
    private int columnarColor = Color.WHITE;
    //控件宽度
    private int width;
    //控件高度
    private int height;
    //当前进度的百分比
    private double percent;
    //动画持续时间
    private long duration = 1500;
    //最大数值
    private double maxValue;
    //当前数值
    private double curValue;
    //字体颜色
    private int textColor = Color.WHITE;
    //字体大小
    private float textSize = 26;
    //文字与柱状条的间距
    private float textMargin = 5;
    //保留2位小数
    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    public HorizontalStatisticalBar(Context context) {
        this(context, null);
    }

    public HorizontalStatisticalBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalStatisticalBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.StatisticalBar);
        columnarColor = ta.getColor(R.styleable.StatisticalBar_columnar_Color, Color.WHITE);
        textSize = ta.getDimension(R.styleable.StatisticalBar_text_Size, 26);
        textColor = ta.getColor(R.styleable.StatisticalBar_text_Color, Color.WHITE);
        ta.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        try {
            //绘制背景柱状条
            drawBackground(canvas);
            //绘制前景柱状条
            drawForeground(canvas);
            //绘制文字
            drawText(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 绘制背景柱状条
     *
     * @param canvas
     */
    private void drawBackground(Canvas canvas) {
        paint.setColor(columnarColor);
        paint.setAlpha(80);

        float left = 0;
        float top = height / 2 - getTextHeight("￥") / 2 - textMargin;
        float right = width;
        float bottom = top + getTextHeight("￥") + textMargin * 2;

        RectF background = new RectF();
        background.left = left;
        background.top = top;
        background.right = right;
        background.bottom = bottom;
        canvas.drawRoundRect(background, 45, 45, paint);
    }

    /**
     * 绘制前景柱状条
     *
     * @param canvas
     */
    private void drawForeground(Canvas canvas) {
        paint.setColor(columnarColor);
        paint.setAlpha(255);

        float left = 0;
        float top = height / 2 - getTextHeight("￥") / 2 - textMargin;
        float right = (float) (width * curValue / maxValue * percent);
        float bottom = top + getTextHeight("￥") + textMargin * 2;

        RectF background = new RectF();
        background.left = left;
        background.top = top;
        background.right = right;
        background.bottom = bottom;
        canvas.drawRoundRect(background, 45, 45, paint);
    }

    /**
     * 绘制文字
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        paint.setColor(textColor);
        paint.setAlpha(255);

        String text = "￥" + twodecimalPlaces(curValue * percent + "");
        float x = (float) (width * curValue / maxValue * percent) - (getTextWidth(text) + textMargin * 4);
        float y = height / 2 + getTextHeight("￥") / 2;

        float indicatorWidth = (float) (width * curValue / maxValue * percent);
        x = getTextWidth(text) >= indicatorWidth ? (indicatorWidth + 2 * textMargin) : x;

        canvas.drawText(text, x, y, paint);
    }

    /**
     * 获取文字的总宽度
     *
     * @return
     */
    private int getTextWidth(String text) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.width();
    }

    /**
     * 获取文字的总高度
     *
     * @return
     */
    private int getTextHeight(String text) {
        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    /**
     * 设置最大进度值
     *
     * @param maxValue
     */
    public void setMaxValue(double maxValue) {
        this.maxValue = maxValue;
    }

    /**
     * 设置当前进度值
     *
     * @param curValue
     */
    public void setCurValue(double curValue) {
        this.curValue = curValue;
        startAnimation();
    }

    /**
     * 保留2位小数
     *
     * @param number
     * @return
     */
    public String twodecimalPlaces(String number) {
        return decimalFormat.format(Double.parseDouble(number));
    }

    /**
     * 开始执行动画
     */
    private void startAnimation() {
        setLayerType(LAYER_TYPE_HARDWARE, null);

        ObjectAnimator an = ObjectAnimator.ofFloat(this, "percent", 0, 1);
        an.setInterpolator(new FastOutSlowInInterpolator());
        an.addListener(this);
        an.setDuration(duration);
        an.start();
    }

    public double getPercent() {
        return percent;
    }

    public void setPercent(float percent) {
        this.percent = percent;
        invalidate();
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        setLayerType(LAYER_TYPE_NONE, null);
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
}
